package com.epam.tcs.dao.hibernate.impl;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.epam.tcs.dao.factory.DAO;
import com.epam.tcs.dao.hibernate.ConnectionFactory;


public abstract class ElementDAOHibernateImpl<T> implements DAO<T> {
	
	protected Class<?> objType;

	@Override
	public long addElement(T elem) {
		Session session = ConnectionFactory.getOrInitSession();
		Transaction trx = session.beginTransaction();
		long id = (Integer) session.save(elem);
		trx.commit();
		session.close();
		return id;
	}

	@Override
	public void updateElement(T elem) {
		Session session = ConnectionFactory.getOrInitSession();
		Transaction trx = session.beginTransaction();
		session.saveOrUpdate(elem);
		trx.commit();
		session.close();
	}

	@Override
	public void deleteElement(T elem) {
		Session session = ConnectionFactory.getOrInitSession();
		Transaction trx = session.beginTransaction();
		session.delete(elem);
		trx.commit();
		session.close();
	}

}
